/**
 * 
 */
package com.et114.modules.products.params;

import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.components.acegi.Constants;
import com.et114.components.simpletable.DataList;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.core.utility.NumberUtils;
import com.et114.core.utility.StringPool;
import com.et114.modules.products.commons.Const;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/*
 * @author guanhw
 */
public class ParamValueAction extends BaseDispatchAction {
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getParamValue( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
		List< BizOption > csOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "csOps" , csOps ) ;
		
		//PARAMS
		ParamValueForm paramForm = ( ParamValueForm ) form ; 
		Long paramId = paramForm.getParamId ( ) ; 
		ParamValue vo = new ParamValue( ) ; 
		PropertyUtils.copyProperties ( vo , paramForm ) ; 
		//BIS
		
		//BIS
		ParamServiceImpl psi = ( ParamServiceImpl )getBean( "paramServiceImpl" ) ; 

		
		if ( !NumberUtils.isEmptyOrZero ( paramId ) ) { //load one
			vo = psi.getParamValueById ( paramId ) ; 
			request.setAttribute ( "vo" , vo ) ; 			
			return mapping.findForward ( "editparamvalue" ) ; 
		}
		else { //loads
			Long typeId = paramForm.getTypeId ( ) ; 
			if ( NumberUtils.isEmptyOrZero ( typeId ) )
				throw new Exception( Constants.SYS_EXP_K + "-typeId is NULL !" ) ; 
			
			DataList dl = psi.getParamValues (  vo ) ; 
			request.setAttribute ( "dl" , dl ) ; 
			request.setAttribute ( "vo" , vo ) ;
			request.setAttribute ( "typeVo" , psi.getParamTypeById ( typeId ) ) ;
			logger.info ( "////" + psi.getParamTypeById ( typeId ).getGlobalKey ( ) ) ;
			return mapping.findForward ( "paramvalues" ) ; 
		}
	}
	
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward toEditParamValue( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		List< BizOption > csOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "csOps" , csOps ) ;
		
		ParamValueForm paramForm = ( ParamValueForm ) form ; 
		Long typeId = paramForm.getTypeId ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( typeId ) )
			throw new Exception( Constants.SYS_EXP_K + "-typeId is NULL !" ) ; 
		
		//BIS
		ParamServiceImpl psi = ( ParamServiceImpl )getBean( "paramServiceImpl" ) ; 
		ParamType type = psi.getParamTypeById ( typeId ) ; 
		request.setAttribute ( "typeVo" , type ) ;
		return mapping.findForward ( "editparamvalue" ) ; 
	}
	
	
	public  ActionForward editParamValue( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
		
		//PARAMS
		ParamValueForm paramForm = ( ParamValueForm ) form ; 
		Long paramId = paramForm.getParamId ( ) ; 
		ParamValue vo = new ParamValue( ) ; 
		PropertyUtils.copyProperties ( vo , paramForm ) ; 
		//BIS
		ParamServiceImpl psi = ( ParamServiceImpl )getBean( "paramServiceImpl" ) ; 

		if ( !NumberUtils.isEmptyOrZero ( paramId ) ) { //update
			psi.updateParamValue ( vo ) ; 
		}
		else { //new 
			paramId = psi.saveParamValue ( vo ) ; 
		}
		return new ActionForward (
				"paramvalue.go?method=getParamValue&paramId=" + paramId , true );
	}	
	
	public  ActionForward removeParamValue( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
		
		//PARAMS
		ParamValueForm paramForm = ( ParamValueForm ) form ; 
		Long typeId = paramForm.getTypeId ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( typeId ) )
			throw new Exception( Constants.SYS_EXP_K + "-typeId is NULL !" ) ; 
		
		//BIS
		ParamServiceImpl psi = ( ParamServiceImpl )getBean( "paramServiceImpl" ) ; 
		ParamType type = psi.getParamTypeById ( typeId ) ; 
		
		ParamValue vo = null ; 
		Long[] deleteIds = paramForm.getDeleteId ( ) ; 
		if ( deleteIds != null ) {
			for ( Long deleteId : deleteIds ) {
				vo = psi.getParamValueById ( deleteId ) ; 
				if ( vo != null ) {
					vo.setRemoveMark ( String.valueOf ( Const.COMMON_REMOVE_MARK_Y ) ) ;
					psi.updateParamValue ( vo ) ; 
				}
			}
		}
		return new ActionForward ( "paramvalue.go?method=getParamValue&typeId="
				+ typeId + "&globalKey" + type.getGlobalKey ( ) , true );
	}
	
	
	public  ActionForward toDisOrderParamValue( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
		
		//PARAMS
		ParamValueForm paramForm = ( ParamValueForm ) form ; 
		Long typeId = paramForm.getTypeId ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( typeId ) )   {
			throw new Exception( "typeId is NULL !" ) ; 
		}
		
		//PARAMS
		
		ParamValue vo = new ParamValue( ) ;
		PropertyUtils.copyProperties ( vo , paramForm ) ; 
		
		//BIS
		ParamServiceImpl psi = ( ParamServiceImpl )getBean( "paramServiceImpl" ) ; 

		List< ParamValue > paramValues = psi.getParamValueList ( vo ) ;
		request.setAttribute ( "pvs" , paramValues ) ;
		request.setAttribute ( "vo" , vo ) ; 
		return mapping.findForward ( "toDisOrderParamValue" ) ; 
	}
	
	public  ActionForward orderParamValue( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC
		
		//PARAMS
		ParamValueForm paramForm = ( ParamValueForm ) form ; 
		Long typeId = paramForm.getTypeId ( ) ; 
		if ( NumberUtils.isEmptyOrZero ( typeId ) )   {
			throw new Exception( "typeId is NULL !" ) ; 
		}
		
		
		//PARAMS
		String orders = paramForm.getOrders ( ) ; 
		
		//BIS
		ParamServiceImpl psi = ( ParamServiceImpl )getBean( "paramServiceImpl" ) ; 
		ParamType type = psi.getParamTypeById ( typeId ) ; 
		
		if ( orders != null  ) {
			StringTokenizer st = new StringTokenizer( orders , StringPool.SEMICOLON ) ;
			int count = st.countTokens ( ) ; 
			ParamValue paramValue = null ; 
			for (  int i = 0 ; i <  count ; i ++ ) {
				Long orderId = Long.valueOf ( st.nextToken() );
				paramValue = psi.getParamValueById ( orderId ) ; 
				paramValue.setDisplOrder ( Long.valueOf ( i + 1 ) ) ;
				psi.updateParamValue ( paramValue ) ;
			}
		}
		return new ActionForward (
				"paramvalue.go?method=toDisOrderParamValue&typeId="
						+ typeId + "&globalKey=" + type.getGlobalKey ( ), true );
	}		
}
